// pages/cardsetdetail/cardsetdetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardno: "",
    cardNum: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.getData()
  },

  getData: function (e) {
    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": "",
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "attendance_card"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
              that.setData({
                cardno:res.data.cardno,
                cardNum: res.data.cardno
              })
        } else {
          wx.showToast({
            title: "获取信息失败",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }

      },
      fail: function (res) {
        wx.showToast({
          title: "获取信息失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  btnScanCode: function (e) {
    var that = this;
    wx.scanCode({
      success(res) {
        console.log(res)
        that.setData({
          cardNum: res.result
        });
      }
    })
  },
  // cardContentInput:function(e){
  //   this.setData({
  //     cardno: e.detail.value

  //   })
  // },
  bingdingcard: function (e) {
    this.bindCardSet()
  },
  bindCardSet: function (e) {

    if (this.data.cardNum.length < 10) {
      wx.showToast({
        title: "请输入正确的卡号",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    } else if (this.data.cardNum == this.data.cardno) {
      wx.showToast({
        title: "您已经绑定该卡号，请勿重复绑定！",
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return;
    }

    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "cardno": that.data.cardNum,
      "schoolid": app.globalData.schoolId,
      "appType":  app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "attendance_bindcard"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: "绑定成功",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        } else if (res.data.status == 2) {
          wx.showToast({
            title: "该卡号已经被注册",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        } else {
          wx.showToast({
            title: "您输入的卡号无效，请联系400-6610-277",
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "操作失败",
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  }

})